package com.zlt.dao.Impl;

import com.zlt.dao.JobDao;
import com.zlt.dto.JobDTO;
import com.zlt.entity.Company;
import com.zlt.entity.Job;
import com.zlt.entity.JobType;
import com.zlt.entity.User;
import com.zlt.util.SqlUtil;
import com.zlt.vo.JobVo;
import com.zlt.vo.Pager;

import java.util.List;
import java.util.Map;

public class JobDaoImpl implements JobDao {
    @Override
    public List<JobType> selectAllType(int parentId) {
        String sql="select jtid,name,parent_id from job_type where parent_id=?";
        return SqlUtil.select(JobType.class,sql,parentId);
    }

    public Long selectCollection(String uid, int jid) {
        String sql = "select count(id) c from collection where uid=? and jid=?";
        return (Long) SqlUtil.select(sql,uid,jid).get(0).get("c");
    }

    @Override
    public int updateCollection(String uid, int jid,int state) {
        String sql = "update collection set state=? where uid=? and jid=?";
        return SqlUtil.update(sql,state,uid,jid);
    }
    @Override
    public Long selectCountJob() {
        String sql="select count(jid) c from job where state=0 ";
        return (Long) SqlUtil.select(sql).get(0).get("c");
    }
    @Override
    public int insertCollection(String uid, int jid,int state) {
        String sql = "insert into collection (uid,jid,state) values (?,?,?)";
        return SqlUtil.update(sql,uid,jid,state);
    }
    @Override
    public Job selectOneJobByJid(Integer jid) {
        String sql="select jid,job_type.jtid,job_type.name,jname,min_salary,max_salary,description,requirement,work_address,count,state from job left join job_type on job.jtid=job_type.jtid where jid=?";
        return SqlUtil.selectOne(Job.class,sql,jid);
    }
    @Override
    public Job selectOne(int jid) {
        String sql = "select jid,jname,min_salary,max_salary,description,requirement,work_address,uid,cid from job where jid=?";
        return SqlUtil.selectOne(Job.class,sql,jid);
    }

    @Override
    public User selectRecruiter(String uid) {
        String sql = "select uid,nickname,photo from user where uid=?";
        return SqlUtil.selectOne(User.class,sql,uid);
    }

    @Override
    public Integer collectionState(String uid, int jid) {
        String sql = "select state from collection where uid=? and jid=?";
        return (Integer) SqlUtil.select(sql,uid,jid).get(0).get("state");
    }

    @Override
    public Long collectionCount(int jid) {
        String sql = "select count(*) c from collection where jid=? and state=0";
        return (Long) SqlUtil.select(sql,jid).get(0).get("c");
    }

    @Override
    public Company selectCompany(int cid) {
        String sql = "select cname,legal_person,address from company where cid=? and state=0";
        return SqlUtil.selectOne(Company.class,sql,cid);
    }

    @Override
    public Job selectJobByJid(Integer jid) {
        String sql="select * from job where jid=?";
        return SqlUtil.selectOne(Job.class,sql,jid);
    }

    @Override
    public Long collectTotalCount(String uid) {
        String sql = "select count(*) c from collection where uid=? and state=0";
        return (Long) SqlUtil.select(sql,uid ).get(0).get("c");
    }

    @Override
    public List<Map<String,Object>> allCollectionJob(Pager<JobVo> pager, String uid) {
        String sql = "select j.jid,j.jname,j.min_salary,j.max_salary,j.work_address,t.name,c.cname from job j left join job_type t on t.jtid = j.jtid " +
                "left join company c on j.cid = c.cid " +
                "left join collection co on co.jid = j.jid where j.state=0 and co.uid=? and co.state=0 order by j.jid desc limit ?,?";
        return SqlUtil.select(sql,uid,(pager.getPage()-1)* pager.getLimit(),pager.getLimit());
    }

    @Override
    public Long defaultTotalCount() {
        String sql = "select count(*) c from job where state=0";
        return (Long) SqlUtil.select(sql).get(0).get("c");
    }

    @Override
    public List<Map<String,Object>> defaultAllJob(Pager<JobVo> pager) {
        String sql = "select j.jid,j.jname,j.min_salary,j.max_salary,j.work_address,t.name,c.cname from job j left join job_type t on t.jtid = j.jtid " +
                "left join company c on j.cid = c.cid where j.state=0 order by j.jid desc limit ?,?";
        return SqlUtil.select(sql,(pager.getPage()-1)* pager.getLimit(),pager.getLimit());
    }

    @Override
    public Company selectCidByUid(String uid,Integer state) {
        String sql="select cid from user_company where uid=? and state=?";
        return SqlUtil.selectOne(Company.class,sql,uid,state);
    }

    @Override
    public boolean insertJob(Job job) {
        return SqlUtil.insert(job)>0;
    }

    @Override
    public JobType selectJobTypeByJid(Integer jtId) {
        String sql="select jtid,name,parent_id from job_type where jtid=?";
        return SqlUtil.selectOne(JobType.class,sql,jtId);
    }

    @Override
    public boolean updateJobInfo(JobDTO jobDTO) {
        String sql="update job set jname=?,min_salary=?,max_salary=?,description=?,requirement=?,work_address=?,count=? where jid=?";
        return SqlUtil.update(sql,jobDTO.getJname(),jobDTO.getMinSalary(),jobDTO.getMaxSalary(),jobDTO.getDescription(),jobDTO.getRequirement(),jobDTO.getWorkAddress(),jobDTO.getCount(),jobDTO.getJid())>0;
    }


    @Override
    public List<Job> selectAllJob(Pager<JobVo> pager,String uid) {
        String sql="select jid,jname,job_type.`jtid`,job_type.`name`,job_type.`parent_id`,min_salary,max_salary,description,requirement,work_address,count,state from job LEFT JOIN job_type on job_type.jtid=job.jtid where uid=? and state=0 or state=1 limit ?,?";
        return SqlUtil.select(Job.class,sql,uid,(pager.getPage()-1)*pager.getLimit(),pager.getLimit());
    }

    @Override
    public List<JobType> selectAllTypes(int parentId) {
        String sql="select jtid,name,parent_id from job_type where 1=1 and parent_id=?";
        return SqlUtil.select(JobType.class,sql,parentId);
    }
    @Override
    public boolean updateJobState(Integer jid, Integer state) {
        String sql="update job  set state=? where jid=?";
        return SqlUtil.update(sql,state,jid)>0;
    }

        @Override
    public JobType selectJobTypeByJtid(Integer jtid) {
        String sql = "select job_type.* from job_type where jtid=?";
        return SqlUtil.selectOne(JobType.class,sql,jtid);
    }

    @Override
    public List<JobType> selectJobTypeByCid(Integer cid) {

        return null;
    }

    @Override
    public List<Job> selectJobByCid(Integer cid) {
        String sql = "select job.* from job where cid=?";
        return SqlUtil.select(Job.class,sql,cid);
    }

    @Override
    public Long selectJobCount(StringBuilder countSql, List params) {
        return (Long) SqlUtil.select(countSql.toString(),params.toArray()).get(0).get("c");
    }

    @Override
    public List<Job> selectJob(StringBuilder sql, List params, Pager<JobVo> pager) {
        params.add((pager.getPage() - 1) * pager.getLimit());
        params.add(pager.getLimit());
        return SqlUtil.select(Job.class,sql.toString(),params.toArray());
    }


}
